﻿<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><%

' usenet-o-matic
' ©2009-2010 Steffest
' History
' 2 September 2009: First version
' 19 maart 2010: added login and updated Newzbin API urls
' 6 april 2010: added nzbmatrix.com support (as Newzbin is going down the drain)

' This script will search reports on Newzbin and/or NZBMatrix and provide a download link to SABnzbd+
' For this to work you need a Newzbin premium account or a NZNMatrix VIP account and a SABnzbd+ installation

'----------------
'config
'----------------

' credentials
site_login = "xxxxxx" 'the password to access this app, leave blank if you don't need a login

newzbin_login = "username"
newzbin_pass = "xxxxxx"

nzbmatrix_username = "username"
nzbmatrix_apikey = "11111111111" 'to be generated on the the NZBMatrix site.


sabnzbd_url = "http://www.myurl.com/sabnzbd/" 'url where your SABnzbd+ is running
sabnzbd_apikey = "11111111111" 'to be found in Config/General in SABnzbd+
sabnzbd_login = "username" ' if any
sabnzbd_pass = "xxxxxx" ' if any

home_url = "http://www.myurl.com/" ' if any: url back to your main app

'----------------
'end config
'----------------


action = request("action")
searchtext = request("searchtext")
searchnewzbin = request("searchnewzbin")
searchnzbmatrix = request("searchnzbmatrix")
searchenglish = request("searchnzbmatrix")
searchcat = request("searchcat")
searchsize = request("searchsize")
searchlan = request("searchlan")
userid = session("userid")

if userid="" or not isnumeric(userid) then userid=0
if searchsize="" or not isnumeric(searchsize) then searchsize=0


if searchtext="" then
	searchlan="English"
	searchsize=800
	searchnewzbin=1
	searchnzbmatrix=1
end if

categories="Any,Movies,Tv,Music"
languages="Any,English"

if action="login" then
	if request("pass") = site_login then
		userid=1
		session("userid") = userid
	end if
end if

if action="add" then
	if userid>0 and site_login<>"" then
	  reportid = request("id")
	  nzbsource = request("source")
	  
	  sabnzbd_download = sabnzbd_url & "api?mode=addid&apikey="&sabnzbd_apikey&"&ma_username=" & sabnzbd_login & "&ma_password=" & sabnzbd_pass & "&name=" & reportid 
	  
	  if nzbsource="nzbmatrix" then
		  reporturl =  "http://nzbmatrix.com/api-nzb-download.php?id=" & reportid & "&username="&nzbmatrix_username&"&apikey=" & nzbmatrix_apikey
		  reporturl = server.URLEncode(reporturl)
		  sabnzbd_download = sabnzbd_url & "api?mode=addurl&apikey="&sabnzbd_apikey&"&ma_username=" & sabnzbd_login & "&ma_password=" & sabnzbd_pass & "&name=" & reporturl
	  end if
	  
	  
	  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
	  xml.Open "GET", sabnzbd_download, False
	  xml.Send
  
	  
	  response.write("Downloading report id " & reportid & ": ")
	  response.write(xml.responsetext)
	  
	  Set xml = nothing
	  response.End()
	end if
		
end if

%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="width=320; initial-scale=1.0; scalable=yes" name="viewport" />
<meta content="Steffest Newzbin-o-matic" name="Description" />
<meta content="Steffest mobile Newzbin-o-matic" name="Keywords" />
<title>Steffest mobile Usenet-o-matic</title>
<link href="main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<script>
// using jQuery - lazy
$(document).ready(function(){
 

$('.linkbar').click(function(e) {
  e.preventDefault();
  $(this).load($(this).attr('href'))
  $(this).addClass('loaded')
});

$('#anewzbin').click(function(e) {
  e.preventDefault();
  $('#newzbin').toggle();
});

$('#anzbmatrix').click(function(e) {
  e.preventDefault();
  $('#nzbmatrix').toggle();
});




});

</script>
</head>
<body>
<% if userid>0 and site_login<>"" then %>
<h1>Usenet-o-matic V0.2</h1>
<div class="menubar">
<% if home_url<>"" then %><a href="<%=home_url%>">Home</a> &bull; <%end if%>
<a href="<%=sabnzbd_url%>m">SABnzbd+</a> &bull; 
<a href="javascript:$('.optionbar').toggle();">Options</a>
</div>
<form>
<div class="optionbar">
Search using <input type="checkbox" name="searchnewzbin" value="1" <%if searchnewzbin=1 then response.write("checked")%>/> Newzbin - <input type="checkbox" name="searchnzbmatrix" value="1" <%if searchnzbmatrix=1 then response.write("checked")%>/> NZBmatrix<br />
Files smaller then <input type="text" name="searchsize" value="<%=searchsize%>" size="6" />MB<br />
Language <select name="searchlan"><%
for each l in split(languages,",")
selected = ""
if searchlan = l then selected = " selected "
response.write("<option "&selected&">" & l & "</option>")
next
%></select><br />
Category <select name="searchcat"><%
for each c in split(categories,",")
selected = ""
if searchcat = c then selected = " selected "
response.write("<option "&selected&">" & c & "</option>")
next
%></select><br />
</div>
<div class="searchbar">
<input type="text" value="<%=searchtext%>" name="searchtext"/><input type="submit" value="search"/>
</div>
</form>
<%

if searchtext <>"" then
	
	if newzbin_login<>"" and searchnewzbin="1" then
	  response.write("<a class=""nzbsource"" href=""#"" id=""anewzbin"">Newzbin</a>")
	  response.write("<div id=""newzbin"">")
	  slist = getnewzbin(searchtext)
	  
	  ' parse XML and get all items with an enclosure
	   Set objXML = Server.CreateObject("MSXML2.DOMDocument")
	   objXML.async = False
	   objXML.validateOnParse = false
	   objXML.resolveExternals = false
	   
	   bAns = objXML.loadXML(slist)
	   if bAns then
		  teller=0
		  set objItemNodes = objXML.DocumentElement.SelectNodes("entry")
		  for each e in objItemNodes
			  title = e.selectSingleNode("title").Text
			  content = e.selectSingleNode("content").xml
			  reportid = e.selectSingleNode("report:id").Text
			  
			  sabnzbd_download = "?action=add&id=" & reportid 
			  
			  passed = true
			  
			  if searchlan="English" then
			  	if instr(content,"Language - German")>1 then passed=false
			  	if instr(content,"Language - French")>1 then passed=false
			  end if
			  
			  
			  if passed then
			  set links = e.selectNodes("link")
			  for each l in links
				  linktype = l.getAttribute("rel")
				  linkurl = l.getAttribute("href")
				  if linktype = "enclosure" then
					  teller=teller+1	
				
						  response.write("<h3>" &  title 	& "</h3>")	
						  response.write(content)		
						  response.write("<a href="""&sabnzbd_download&""" class=""linkbar"">Download</a>")
				  end if
			  next
			  
			  set links = nothing
			  end if
		  next
		  
		  
	   else
		  response.write("error while parsing feed")
	   end if
	   
	   if teller=0 then
		  response.write("No results on newzbin...")
	   end if
	   
	  
	  response.write("</div")
	 
	 end if
	 
	 if nzbmatrix_username<>"" and searchnzbmatrix="1" then
		response.write("<a class=""nzbsource"" href=""#"" id=""anzbmatrix"">NZBmatrix</a>")
	    response.write("<div id=""nzbmatrix"">")
	  
	 	slist = getnzbmatrix(searchtext)
		
		' parse XML 
	   Set objXML = Server.CreateObject("MSXML2.DOMDocument")
	   objXML.async = False
	   objXML.validateOnParse = false
	   objXML.resolveExternals = false
	   
	   bAns = objXML.loadXML(slist)
	   if bAns then
		  teller=0
		  set objItemNodes = objXML.DocumentElement.selectSingleNode("channel").SelectNodes("item")
		  for each e in objItemNodes
			  title = e.selectSingleNode("title").Text
			  content = e.selectSingleNode("description").Text
			  reportid = e.selectSingleNode("guid").Text
			  p=instr(reportid,"id=")
			  if p>0 then 
				  reportid = mid(reportid,p+3)
				  p=instr(reportid,"&")
				  if p>0 then reportid=left(reportid,p-1)
			  end if
			  
			  sabnzbd_download = "?action=add&source=nzbmatrix&id=" & reportid 
			  
			  passed = true
			  
			 
			  
			  
			  if passed then
			  
					  teller=teller+1	
						  response.write("<h3>" &  title 	& "</h3>")	
						  response.write(content)		
						  response.write("<a href="""&sabnzbd_download&""" class=""linkbar"">Download</a>")
				 
			  end if
		  next
		  
		  
	   else
		  response.write("error while parsing feed")
	   end if
	   
	   if teller=0 then
		  response.write("No results on NZBMAtrix...")
	   end if
	   
	   response.write("</div")
	 end if
	 
	 
end if
%>
<% else %>
<center>
<form action="default.asp?action=login" method="post"><br />
<br />
<br />

Please log in<br />
<input type="password" name="pass"/> <input type="submit" value="  GO  " />
</form>
</center>
<% end if %>
</body>
</html>
<%

function getnewzbin(tag)

tag = server.urlencode(tag)
	
	
	
	rssurl = "http://www.newzbin.com/search/query/?q="&tag&"&area=-1&fpn=p&searchaction=Go&areadone=-1&u_post_results_amt=100&u_v3_retention=0&u_post_smaller_than="&searchsize&"&u_post_states=0&u_completions=11&apply=Apply&u_nfo_posts_only=0&u_url_posts_only=0&u_comment_posts_only=0&u_show_passworded=0&u_use_exclusions=0&feed=atom&hauth=1"
	
	
	Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
	xml.Open "GET", rssurl, False, newzbin_login, newzbin_pass
	xml.setRequestHeader "Content-Type", "text/xml"
	xml.Send

	
	getnewzbin = xml.responsetext
		
	Set xml = Nothing


end function


function getnzbmatrix(tag)

	tag = server.urlencode(tag)

	' search api url - seems pretty broken at the moment
	rssurl = "http://nzbmatrix.com/api-nzb-search.php?search="&tag&"&username="&nzbmatrix_username&"&apikey="&nzbmatrix_apikey
	
	' download RSS
	rssurl = "http://rss.nzbmatrix.com/rss.php?page=download&username="&nzbmatrix_username&"&apikey="&nzbmatrix_apikey&"&subcat=&term=" & tag
	
	
	'info RSS
	lanparam = ""
	if searchlan="English" then lanparam="&english=1"
	subcat=""
	if searchcat = "Tv" then subcat=6
	rssurl = "http://rss.nzbmatrix.com/rss.php?page=details&subcat="&subcat&lanparam&"&term=" & tag
	
	
	Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
	xml.Open "GET", rssurl, False
	xml.setRequestHeader "Content-Type", "text/xml"
	xml.Send

	
	getnzbmatrix = xml.responsetext
		
	Set xml = Nothing


end function


%>
